//==========================================================================
// Copyright (c) 2000-2009,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#ifndef _PPPDRV_H_
#define _PPPDRV_H_

#include "internal.h"


typedef unsigned short  u16_t;

struct pbuf
{
    struct pbuf *next;
    void *payload;
    void *limit;
    u16_t tot_len;
    u16_t len;
    u16_t flags;
    u16_t ref;
};

enum pppAuthType
{
    PPPAUTHTYPE_NONE,
    PPPAUTHTYPE_ANY,
    PPPAUTHTYPE_PAP,
    PPPAUTHTYPE_CHAP
};

typedef struct _PPPAuthData
{
    enum pppAuthType authType;
    char user[32];
    char passwd[32];
} PPPAuthData;

typedef struct _PPPAddr
{
    UInt32 our;
    UInt32 host;
    UInt32 mask;
    UInt32 dns1;
    UInt32 dns2;
} PPPAddr;

typedef struct _PPPIoctlData
{
   UInt32 pd;
   UInt32 cmd;
   UInt32 arg;
} PPPIoctlData;

typedef struct _PPPIoctlWAITEVENT
{
   UInt32 type;
   UInt32 timeout;
} PPPIoctl_WaitEvent;
typedef struct _PPPBuffer
{
   Int32 fd;
   Int32 size;
   Byte buff[1];
} PPPBuffer;

#define PPP_EVENT_UPLINK_REQ   0x0001

//control code
#define PPP_INIT           100
#define PPP_SETAUTH        200
#define PPP_OPEN           300
#define PPP_CLOSE          400
#define PPP_IOCTOL         500
#define PPP_SETADDR        700
#define PPP_GETADDR        800
#define PPP_GETDNS         801
#define PPP_WAITUP         900
#define PPP_READABORT      901
#define PPP_WAITEVENT      1000


//
// PppTran
//
class PppTran : public CDeviceDriver
{

public:
    CARAPI Read(
        /* [in] */ Int64 u64Offset,
        /* [in] */ Int32 bytesToRead,
        /* [out] */ MemoryBuf * pBuffer,
        /* [out] */ IEvent * * ppCompletionEvent);

    CARAPI Write(
        /* [in] */ Int64 u64Offset,
        /* [in] */ const MemoryBuf & buffer,
        /* [out] */ Int32 * pBytesWritten,
        /* [out] */ IEvent * * ppCompletionEvent);

    CARAPI Control(
        /* [in] */ Handle32 nControlCode,
        /* [in] */ const MemoryBuf & inBuffer,
        /* [out] */ MemoryBuf * pOutBuffer,
        /* [out] */ IEvent * * ppCompletionEvent);

    virtual void Dispose();

public:
    PppTran();
    ~PppTran();
    CARAPI Init();
    int pppUplink(Int32 fd, struct pbuf *nb);
private:
    BoolT enqueue(Int32 fd, struct pbuf *nb);
    BoolT dequeue(Int32 *fd, struct pbuf **nb);

public:
    PPP_LINKNODE m_pppLink[64];
    IntT m_head, m_tail;
    IntT m_count;
    DzMutex m_pppLinkLock;
    IEvent *m_pppLinkEvent;
};

#endif

